package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.City;
import com.example.buildtable.service.CityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "城市")
@RequestMapping(value = "/city/")
public class CityController {
    @Autowired
    private CityService cityService;
    
    @Operation(summary = "查询所有城市数据")
    @GetMapping(value = "/getAllCity")
    public R getAllCity() {
        List<City> cityList = cityService.list();
        return R.ok("查询所有城市数据成功").data("cityList", cityList);
    }
    
    @Operation(summary = "增加城市")
    @PostMapping(value = "/addCity")
    public R addCity(City city) {
        boolean flag = cityService.save(city);
        if (flag) {
            return R.ok("增加城市成功");
        } else {
            return R.ok("增加城市失败");
        }
    }

    @Operation(summary = "删除城市")
    @GetMapping(value = "/deleteCity/{cityId}")
    public R deleteCity(@PathVariable("cityId") Integer cityId) {
        boolean flag = cityService.removeById(cityId);
        if (flag) {
            return R.ok("删除城市成功");
        } else {
            return R.ok("删除城市失败");
        }
    }

    @Operation(summary = "按照城市查看")
    @GetMapping(value = "/getCityById/{cityId}")
    public R getCityById(@PathVariable("cityId") Integer cityId) {
        City city = cityService.getById(cityId);
        if (city != null) {
            return R.ok("按照城市查看成功").data("city", city);
        } else {
            return R.error("按照城市查看失败");
        }
    }

    @Operation(summary = "修改城市")
    @PostMapping(value = "/updateCity")
    public R updateCity(City city) {
        boolean flag = cityService.updateById(city);
        if (flag) {
            return R.ok("城市修改成功");
        } else {
            return R.ok("城市修改失败");
        }
    }
}